import json
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.sms.v20210111 import sms_client, models
import os,django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "war_djago.settings")# project_name 项目名称
django.setup()

from django.conf import settings
def send_sms(ph_number,sms_code,templateid):
    try:
        # 实例化一个认证对象，入参需要传入腾讯云账户secretId，secretKey,此处还需注意密钥对的保密
        # 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
        cred = credential.Credential(secret_id=settings.SMS_SECRET_ID,secret_key=settings.SMS_SECRET_KEY)
        # 实例化一个http选项，可选的，没有特殊需求可以跳过
        httpProfile = HttpProfile()
        httpProfile.endpoint = "sms.tencentcloudapi.com"

        # 实例化一个client选项，可选的，没有特殊需求可以跳过
        clientProfile = ClientProfile()
        clientProfile.httpProfile = httpProfile
        # 实例化要请求产品的client对象,clientProfile是可选的
        client = sms_client.SmsClient(cred, "ap-guangzhou", clientProfile)

        # 实例化一个请求对象,每个接口都会对应一个request对象
        req = models.SendSmsRequest()
        #req.TemplateParamSet=['239465']
        params = {
            "PhoneNumberSet": ph_number,
            "SmsSdkAppId": settings.TENCENT_SMS_SDK_APPID,
            "TemplateParamSet": sms_code,
            "SessionContext": "您的动态验证码为：{1}，您正在进行密码重置操作，如非本人操作，请忽略本短信！--你的壮geigei",
            "SignName": settings.TENCENT_SMS_SIGN,
            "TemplateId": templateid
        }
        req.from_json_string(json.dumps(params))

        # 返回的resp是一个SendSmsResponse的实例，与请求对象对应
        resp = client.SendSms(req)
        # 输出json格式的字符串回包
        print(resp)
        return resp.to_json_string()
    except TencentCloudSDKException as err:
        print(err)
import random
if __name__ == '__main__':
    #输入腾讯云服务ID与密钥
    ph_number_ie= '+86'
    number = '18567185931'
    ph_number=[]
    ph_number.append(ph_number_ie+number)
    code_ie = str(random.randrange(100000, 999999))
    code=[]
    code.append(code_ie)
    print(type(ph_number),ph_number)
    #send_sms(ph_number,['204781',],'1492850')